package com.zzptc.ygz;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Several {
        private DefaultListModel dt;
        private JTextArea jta;
        private JTextField jtf_message = new JTextField(30);
        private JTextField jtf_port = new JTextField("9999");
        private JTextField jtf_max = new JTextField("30");
        private JButton jbt_start = new JButton("启动");
        private JButton jbt_stop = new JButton("停止");
        private List<Socket>list= new ArrayList<>();

        public void createUI(){
            JFrame jf = new JFrame("服务器");
            jf.setBounds(200,200,500,300);
            //北边Jpanel
            JPanel northPane = new JPanel();
            northPane.setLayout(new GridLayout(1,6));
            northPane.add(new JLabel("人数上限"));
            northPane.add(jtf_max);
            northPane.add(new JLabel("端口"));
            northPane.add(jtf_port);
            northPane.add(jbt_start);
            jbt_stop.setEnabled(false);
            northPane.add(jbt_stop);
            northPane.setBorder(new TitledBorder("配置信息"));
            //中间Jpanl(左右分栏)
            //左栏
            dt = new DefaultListModel();
            JList jl = new JList(dt);
            JScrollPane leftpane = new JScrollPane(jl);
            leftpane.setBorder(new TitledBorder("在线用户"));
            //右栏
            jta = new JTextArea();
            jta.setEnabled(false);
            JScrollPane reghtpane = new JScrollPane(jta);
            reghtpane.setBorder(new TitledBorder("消息显示区"));
            JSplitPane centPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftpane,reghtpane);
            //设置分割条大小位置
            centPane.setDividerSize(20);
            centPane.setDividerLocation(100);
            //南部Jpanel
            JPanel southPanel = new JPanel();
            southPanel.setBorder(new TitledBorder("写消息"));
            southPanel.add(jtf_message,BorderLayout.CENTER );
            jf.setLayout(new BorderLayout());
            jf.add(northPane,BorderLayout.NORTH);
            jf.add(centPane,BorderLayout.CENTER);
            jf.add(southPanel,BorderLayout.SOUTH);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setVisible(true);
            //服务器界面
            myEvent();
    }

    private void myEvent() {
        jbt_start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                     ServerSocket ss  = new ServerSocket(9999);
                new Thread(new Runnable() {//创建线程
                    @Override
                    public void run() {
                        try {
                            while (true){
                            Socket socket = ss.accept();
                            list.add(socket);
                            dt.addElement("用户"+list.size());
                            InputStream in =socket.getInputStream();
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                           new Thread(new Runnable() {
                               @Override
                               public void run() {
                                   while (true){
                                       String str = null;
                                       try {
                                           str = bufferedReader.readLine().trim();
                                       } catch (IOException e1) {
                                           e1.printStackTrace();
                                       }
                                       jta.append(str+"\n");
                                   }
                               }
                           }).start();
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }).start();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            }
        });


        jtf_message.addKeyListener( new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyChar()==KeyEvent.VK_ENTER){
                    for(Socket s:list) {
                        try {
                            OutputStream in = s.getOutputStream();
                            //PrintStream ps = new PrintStream(in);
                            //ps.println(jtf_message.getText());
                            PrintWriter pw = new PrintWriter(in,true);
                            pw.println(jtf_message.getText());
                            jta.append(jtf_message.getText()+"\n");
                            pw.flush();
                           jtf_message.setText("");
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        new Several().createUI();
    }
}
